package com.atguigu.aliyunvod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.aliyunvod.service.VodService;
import com.atguigu.aliyunvod.util.AliyunVodSDKUtils;
import com.atguigu.aliyunvod.util.ConstantVodUtils;
import com.atguigu.servicebase.config.exceptionHandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author LHL
 * @Date 23-3-4-0004 14:50
 * @注释
 */
@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVIdeo(MultipartFile file) {
            try {
                //accessKeyId,accessKeySecret
                System.out.println("ACCESS_KEY_ID " + ConstantVodUtils.ACCESS_KEY_ID);
                System.out.println("ACCESS_KEY_SECRET " + ConstantVodUtils.ACCESS_KEY_SECRET);
                //fileName：上传文件原始名称
                String fileName = file.getOriginalFilename();
                System.out.println("fileName " + fileName);

                //title：上传之后显示名称
                String title = fileName.substring(0, fileName.lastIndexOf("."));

                //inputStream：上传文件的输入流
                InputStream inputStream = file.getInputStream();

                UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID
                        , ConstantVodUtils.ACCESS_KEY_SECRET
                        , title, fileName
                        , inputStream);
                request.setStorageLocation("outin-79c0b14fe1f211eda60c00163e10fc78.oss-cn-shenzhen.aliyuncs.com");
                request.setApiRegionId("cn-shenzhen");
                UploadVideoImpl uploader = new UploadVideoImpl();
                UploadStreamResponse response = uploader.uploadStream(request);
                System.out.println("response " + response);
                System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
                String videoId = null;
                if (response.isSuccess()) {
                    videoId = response.getVideoId();
                    System.out.println("videoId " + videoId);
                } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                    videoId = response.getVideoId();
                }
                // if(StringUtils.isEmpty(videoId)){
                //     throw new Exception(20001, "上传视频失败");
                // }
                return videoId;

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

    @Override
    public void removeVideo(String videoId) {
        try {
            //初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request中设置videoId
            request.setVideoIds(videoId);
            //调用删除方法
            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");

        } catch (ClientException e) {
            throw new GuliException(20001, "视频删除失败");
        }
    }

    @Override
    public void removeAllVideo(List videoIdList) {
        try {
            //初始化对象

            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //遍历数组为 1,2,3,4
            String join = StringUtils.join(videoIdList.toArray(), ",");

            //向request中设置videoId
            request.setVideoIds(join);
            //调用删除方法
            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");

        } catch (ClientException e) {
            throw new GuliException(20001, "视频删除失败");
        }
    }
}
